﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralLibs;
using Lists;
using CustomSecurity;

namespace ListForms._1Clinic
{
    public partial class UcTongTienThanhToan : UserControl
    {
        public UcTongTienThanhToan()
        {
            InitializeComponent();
        }

        private void Uc_Load(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }

        }

        private void txtTienThanhToan_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtTienKhachDua.EditValue is decimal && txtTienThanhToan.EditValue is decimal)
                {
                    if ((decimal)txtTienKhachDua.EditValue >= (decimal)txtTienThanhToan.EditValue)
                    {
                        lblTienThua.Text = "Tiền thừa";
                        txtTienTraLai.EditValue = (decimal)txtTienKhachDua.EditValue - (decimal)txtTienThanhToan.EditValue;
                    }
                    else
                    {
                        lblTienThua.Text = "Tiền thiếu ";
                        txtTienTraLai.EditValue = (decimal)txtTienThanhToan.EditValue - (decimal)txtTienKhachDua.EditValue;
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void txtTienKhachDua_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtTienKhachDua.EditValue is decimal && txtTienThanhToan.EditValue is decimal)
                {
                    if ((decimal)txtTienKhachDua.EditValue >= (decimal)txtTienThanhToan.EditValue)
                    {
                        lblTienThua.Text = "Tiền thừa";
                        txtTienTraLai.EditValue = (decimal)txtTienKhachDua.EditValue - (decimal)txtTienThanhToan.EditValue;
                        txtTienTraLai.BackColor = Color.SkyBlue;
                    }
                    else
                    {
                        lblTienThua.Text = "Tiền thiếu ";
                        txtTienTraLai.EditValue = (decimal)txtTienThanhToan.EditValue - (decimal)txtTienKhachDua.EditValue;
                        txtTienTraLai.BackColor = Color.LightSalmon;
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void txtPhanTramCK_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                decimal phantramCK = 0;
                decimal tienCK = 0;
                decimal tienGiaLe = 0;
                decimal tienThanhToan = 0;

                if (txtPhanTramCK.EditValue != null && txtPhanTramCK.EditValue.ToString().Trim() != "")
                    phantramCK = Convert.ToDecimal(txtPhanTramCK.EditValue.ToString().Trim());
                if (txtTienChietKhau.EditValue != null && txtTienChietKhau.EditValue.ToString().Trim() != "")
                    tienCK = Convert.ToDecimal(txtTienChietKhau.EditValue.ToString().Trim());
                if (txtTienGiaLe.EditValue != null && txtTienGiaLe.EditValue.ToString().Trim() != "")
                    tienGiaLe = Convert.ToDecimal(txtTienGiaLe.EditValue.ToString().Trim());
                if (txtTienThanhToan.EditValue != null && txtTienThanhToan.EditValue.ToString().Trim() != "")
                    tienThanhToan = Convert.ToDecimal(txtTienThanhToan.EditValue.ToString().Trim());

                if (phantramCK != 0)
                {
                    if (txtTienChietKhau.EditValue == null || tienCK != tienGiaLe * (phantramCK / 100))
                        txtTienChietKhau.EditValue = tienGiaLe * (phantramCK / 100);
                    if (txtTienThanhToan.EditValue == null || tienThanhToan != tienGiaLe - tienCK)
                        txtTienThanhToan.EditValue = tienGiaLe - tienCK;
                }
                else
                {
                    txtTienThanhToan.EditValue = (decimal)txtTienGiaLe.EditValue;
                    txtTienChietKhau.EditValue = 0;
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void txtTienChietKhau_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                decimal phantramCK = 0;
                decimal tienCK = 0;
                decimal tienGiaLe = 0;
                decimal tienThanhToan = 0;

                if (txtPhanTramCK.EditValue != null && txtPhanTramCK.EditValue.ToString().Trim() != "")
                    phantramCK = Convert.ToDecimal(txtPhanTramCK.EditValue.ToString().Trim());
                if (txtTienChietKhau.EditValue != null && txtTienChietKhau.EditValue.ToString().Trim() != "")
                    tienCK = Convert.ToDecimal(txtTienChietKhau.EditValue.ToString().Trim());
                if (txtTienGiaLe.EditValue != null && txtTienGiaLe.EditValue.ToString().Trim() != "")
                    tienGiaLe = Convert.ToDecimal(txtTienGiaLe.EditValue.ToString().Trim());
                if (txtTienThanhToan.EditValue != null && txtTienThanhToan.EditValue.ToString().Trim() != "")
                    tienThanhToan = Convert.ToDecimal(txtTienThanhToan.EditValue.ToString().Trim());

                if (tienCK != 0)
                {
                    if (txtPhanTramCK.EditValue == null || phantramCK != (tienCK * 100) / tienGiaLe)
                        txtPhanTramCK.EditValue = (tienCK * 100) / tienGiaLe;

                    if (txtTienThanhToan.EditValue != null || tienThanhToan != tienGiaLe - tienCK)
                        txtTienThanhToan.EditValue = tienGiaLe - tienCK;
                }
                else
                {
                    txtTienThanhToan.EditValue = (decimal)txtTienGiaLe.EditValue;
                    txtPhanTramCK.EditValue = 0;
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void txtTienGiaLe_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtPhanTramCK.EditValue is decimal && (decimal)txtPhanTramCK.EditValue != (decimal)0)
                {
                    txtTienChietKhau.EditValue = (decimal)txtTienGiaLe.EditValue * ((decimal)txtPhanTramCK.EditValue / 100);
                    txtTienThanhToan.EditValue = (decimal)txtTienGiaLe.EditValue * (decimal)txtTienChietKhau.EditValue;
                }
                else if (txtTienChietKhau.EditValue is decimal && (decimal)txtTienChietKhau.EditValue != (decimal)0)
                {
                    txtPhanTramCK.EditValue = (decimal)txtTienChietKhau.EditValue / ((decimal)txtTienGiaLe.EditValue * 100);
                    txtTienThanhToan.EditValue = (decimal)txtTienGiaLe.EditValue * (decimal)txtTienChietKhau.EditValue;
                }
                else
                {
                    txtTienThanhToan.EditValue = txtTienGiaLe.EditValue;
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }
    }
}
